package com.lzb.yabbs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzb.yabbs.R;
import com.lzb.yabbs.dao.Board;
import com.lzb.yabbs.interfaces.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuzaibing on 2015/9/9.
 */
public class BoardAdapter extends  RecyclerView.Adapter<BoardAdapter.MyViewHolder>{

    private List<Board> mBoards=new ArrayList<>();
    private Context mContext;

    public BoardAdapter(List<Board> mBoards, Context mContext) {
        this.mBoards = mBoards;
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView= LayoutInflater.from(mContext).inflate(R.layout.board_item,parent,false);

        return new MyViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.boardNameTv.setText(mBoards.get(position).getName());
        holder.themeNumTv.setText("今日:"+mBoards.get(position).getPost_num());
        holder.todayNumTv.setText("主题:"+mBoards.get(position).getPost_today());
        holder.descriptionTv.setText(mBoards.get(position).getDescription());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(position);
            }
        });
    }

    public void addBoards(List<Board> mBoards){
        this.mBoards.clear();
        this.mBoards.addAll(mBoards);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mBoards.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView boardNameTv;
        TextView descriptionTv;
        TextView todayNumTv;
        TextView themeNumTv;
        View itemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            boardNameTv= (TextView) itemView.findViewById(R.id.board_name_tv);
            descriptionTv= (TextView) itemView.findViewById(R.id.description_tv);
            todayNumTv= (TextView) itemView.findViewById(R.id.today_post_num);
            themeNumTv= (TextView) itemView.findViewById(R.id.theme_num_tv);
        }
    }
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener=mOnItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;
}
